﻿using Dimeng.ArdisPostProcessor.HomagLC4.Ardis;
using ReadR41;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dimeng.ArdisPostProcessor.HomagLC4Adsn
{
    /// <summary>
    /// 把切割图形，转换为png文件
    /// </summary>
    public class DrawingHelper
    {
        public static void Draw(string filename, List<ExternCut> allcuts, Pattern pattern)
        {
            int ratio = 4;
            int length = (int)(pattern.PatternL / ratio);
            int width = (int)(pattern.PatternW / ratio);
            int border = 20;

            Bitmap objBitmap = null;
            Graphics g = null;
            objBitmap = new Bitmap(length + 2 * border, width + 2 * border);
            using (Pen pen = new Pen(new SolidBrush(Color.Black)))
            using (g = Graphics.FromImage(objBitmap))
            {
                //起点
                float startx = border;
                float starty = border;

                //尺寸
                float pLength = (float)pattern.PatternL;
                float pWidth = (float)pattern.PatternW;

                //先画大板
                g.DrawRectangle(pen, startx, starty, pLength/ratio, pWidth/ratio);

                //取出第一阶的cuts
                var level1Cuts = allcuts.Where(it => it.CutLevel == 1).ToList();
                drawCuts(g, pen, level1Cuts, pLength, pWidth, startx, starty, allcuts,ratio);
            }
            //SizeF stringSize = g.MeasureString(_ShowName, stringFont);
            //int nWidth = (int)stringSize.Width;
            //int nHeight = (int)stringSize.Height;

            //g.FillRectangle(new SolidBrush(Color.Yellow), new Rectangle(0, 0, nWidth, nHeight));
            //g.FillEllipse(new SolidBrush(Color.Yellow), new Rectangle(0, 0, nWidth, nHeight));
            //g.DrawString(_ShowName, stringFont, new SolidBrush(Color.Black), new PointF(0, 0), stringFormat);
            objBitmap.Save(filename, ImageFormat.Png);

            return;
        }

        private static void drawCuts(Graphics g, Pen pen, List<ExternCut> cuts, float pLength, float pWidth, float x, float y, List<ExternCut> allcuts,int ratio)
        {
            float x0 = x;
            float y0 = y;
            float l = pLength;
            float w = pWidth;

            for (int i = 0; i < cuts.Count; i++)
            {
                var c = cuts[i];

                if (c.CutType == CutType.SawCut)
                {
                    continue;//跳过SawCut
                }

                if (c.CutD == 'H')
                {
                    l = (float)c.CutW;
                }
                else
                {
                    w = (float)c.CutW;
                }

                g.DrawRectangle(pen, x0, y0, l/ratio, w/ratio);

                if (c.CutType==CutType.Strip)
                {
                    drawCuts(g, pen, allcuts.Where(it => it.CutParentNo == c.CutNo).ToList(), l, w, x0, y0, allcuts,ratio);
                }


                //注意x0和y0计算放在后面，避免影响之前的绘制
                if (c.CutD == 'H')
                {
                    x0 += (float)c.CutW / ratio;
                }
                else
                {
                    y0 += (float)c.CutW / ratio;
                }
            }
        }
    }
}
